/**
 * Created by ranchenguang on 2017/09/14.
 * 每个页面对应一个 module 每个子页面对应一个 store js 文件
 */
import Util from '../../../util'
import * as types from '../../mutation-types'
import {Toast, Indicator} from 'mint-ui';

const state = {
  isLogin: false,
  sendVerify:false
};

const getters = {
  //基础数据
  Login_isLogin: state => state.isLogin,
  Register_sendVerify: state => state.sendVerify,
};

//每个action包含一个promise请求，方便回调
const actions = {
  'Login/login'({commit}, params) {
    commit(types.LOGIN_IS_LOGIN, false);
    Indicator.open({
      text: '登录中...',
      spinnerType: 'snake'
    });
    return Util.createPromise('login', params, 'post').then((data) => {
      Indicator.close();
      if (data !== void 0) {
        commit(types.LOGIN_IS_LOGIN, true);
      }
      return data;
    })
  },
  'Register/register'({commit}, params) {
    Indicator.open({
      text: '注册中...',
      spinnerType: 'snake'
    });
    return Util.createPromise('register', params, 'post').then((data) => {
      Indicator.close();
      return data;
    })
  },
  'Register/verifyPhone'({commit}, params) {
    return Util.createPromise('verifyPhone', params).then((data) => {
      return data;
    })
  },
  'Register/getVerify'({commit}, params) {
    return Util.createPromise('smsCode', params, 'post').then((data) => {
      if (data !== void 0) {
        commit(types.REGISTER_SEND_VERIFY, true);
        Toast({
          message: '验证码已发送',
          position: 'bottom',
          duration: 3000
        })
      }
      return data;
    })
  }
};

const mutations = {
  [types.LOGIN_IS_LOGIN](state, data) {
    state.isLogin = data;
  },
  [types.REGISTER_SEND_VERIFY](state, data) {
    state.sendVerify = data;
  },
};

export default {
  state,
  getters,
  actions,
  mutations
};